﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 获取可以使用的表列表
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("ETL", "GetMappedDataTableList", "1.0")]
    public class GetMappedDataTableListAction : BaseAction<GetMappedDataTableListParams, GetMappedDataTableListResult>
    {
        protected override ActionResult<GetMappedDataTableListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetMappedDataTableListParams opParams)
        {
            SER.DtTableQueryService service = new SER.DtTableQueryService();
            PagedList<ENT.EDtTable> result = service.GetMappedTables(opParams.Filter, opParams.Page* 15, 15);
            if(result.IsSuccess)
            {
                GetMappedDataTableListResult actResult = new GetMappedDataTableListResult();
                actResult.Count = result.Data.Count;
                actResult.Total = result.Total;
                actResult.Limit = result.Limit;
                actResult.Start = result.Start;
                actResult.Page = opParams.Page;

                foreach (var eItem in result.Data)
                {
                    GetMappedDataTableListResult.Item item = new GetMappedDataTableListResult.Item();
                    item.Id = eItem.Id;
                    item.Name = eItem.Name;
                    item.Creator = eItem.Creator;
                    item.CreatedTime = eItem.CreatedTime;
                    item.IsRawDt = eItem.IsRawDt;
                    actResult.Items.Add(item);
                }
                return new ActionResult<GetMappedDataTableListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetMappedDataTableListResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class GetMappedDataTableListParams
    {
        public string Filter { get; set; }
        public int Page { get; set; }
    }

    public class GetMappedDataTableListResult : PagedResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Creator { get; set; }

            public DateTime CreatedTime { get; set; }

            public bool IsRawDt { get; set; }
        }
    }
}
